/*
 * AddressRangeDialog.java
 * 
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fpdev.apps.admin.gui.dialogs;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import org.fpdev.core.basenet.BLinkStreet;
import org.fpdev.apps.admin.AdminClient;

public class AddressRangeDialog extends JDialog implements ActionListener {

  private AdminClient cv_;
  private BLinkStreet link_;
  private JTextField fAddrR_,  fAddrL_,  tAddrR_,  tAddrL_,  zipL_,  zipR_;
  private JButton okBtn_,  cancelBtn_;

  public AddressRangeDialog(AdminClient cv, BLinkStreet link) {
    super(cv.getGUI(), "Set Link Address Range");

    cv_ = cv;
    link_ = link;

    fAddrR_ = new JTextField("" + link.getFAddrR());
    fAddrR_.setPreferredSize(new Dimension(40, 20));
    fAddrL_ = new JTextField("" + link.getFAddrL());
    fAddrL_.setPreferredSize(new Dimension(40, 20));
    tAddrR_ = new JTextField("" + link.getTAddrR());
    tAddrR_.setPreferredSize(new Dimension(40, 20));
    tAddrL_ = new JTextField("" + link.getTAddrL());
    tAddrL_.setPreferredSize(new Dimension(40, 20));
    zipR_ = new JTextField("" + link.getZipR());
    zipR_.setPreferredSize(new Dimension(40, 20));
    zipL_ = new JTextField("" + link.getZipL());
    zipL_.setPreferredSize(new Dimension(40, 20));

    okBtn_ = new JButton("OK");
    okBtn_.addActionListener(this);
    cancelBtn_ = new JButton("Cancel");
    cancelBtn_.addActionListener(this);

    JPanel rightRow = new JPanel();
    rightRow.setLayout(new BoxLayout(rightRow, BoxLayout.X_AXIS));
    JLabel rightLabel = new JLabel("Right:");
    rightLabel.setPreferredSize(new Dimension(50, 16));
    rightRow.add(rightLabel);
    rightRow.add(fAddrR_);
    rightRow.add(new JLabel(" to "));
    rightRow.add(tAddrR_);
    rightRow.add(new JLabel(", "));
    rightRow.add(zipR_);

    JPanel leftRow = new JPanel();
    leftRow.setLayout(new BoxLayout(leftRow, BoxLayout.X_AXIS));
    JLabel leftLabel = new JLabel("Left:");
    leftLabel.setPreferredSize(new Dimension(50, 16));
    leftRow.add(leftLabel);
    leftRow.add(fAddrL_);
    leftRow.add(new JLabel(" to "));
    leftRow.add(tAddrL_);
    leftRow.add(new JLabel(", "));
    leftRow.add(zipL_);

    JPanel btnRow = new JPanel();
    btnRow.setLayout(new BoxLayout(btnRow, BoxLayout.X_AXIS));
    btnRow.add(okBtn_);
    btnRow.add(Box.createHorizontalStrut(5));
    btnRow.add(cancelBtn_);

    JPanel topPanel = new JPanel();
    topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));

    leftRow.setAlignmentX(LEFT_ALIGNMENT);
    topPanel.add(leftRow);
    btnRow.add(Box.createVerticalStrut(5));
    rightRow.setAlignmentX(LEFT_ALIGNMENT);
    topPanel.add(rightRow);

    JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

    topPanel.setAlignmentX(CENTER_ALIGNMENT);
    mainPanel.add(topPanel);
    mainPanel.add(Box.createVerticalStrut(6));
    btnRow.setAlignmentX(CENTER_ALIGNMENT);
    mainPanel.add(btnRow);

    mainPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    getContentPane().add(mainPanel);

    this.pack();
    this.setLocation((cv.getGUI().getWidth() / 2) - getWidth() / 2 + cv.getGUI().getX(),
            (cv.getGUI().getHeight() / 2) - getHeight() / 2 + cv.getGUI().getY());
    this.setVisible(true);
  }

  @Override
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == okBtn_) {
      this.setVisible(false);
      cv_.getNetworkOps().updateLinkAddressRange(link_,
              new Integer(fAddrL_.getText()).intValue(),
              new Integer(fAddrR_.getText()).intValue(),
              new Integer(tAddrL_.getText()).intValue(),
              new Integer(tAddrR_.getText()).intValue(),
              new Integer(zipL_.getText()).intValue(),
              new Integer(zipR_.getText()).intValue());

    //cv_.renameSelectedLinks(name_.getText());
    } else if (e.getSource() == cancelBtn_) {
      this.setVisible(false);
    }
  }
}



























